<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/28 0028
 * Time: 下午 16:08
 */
namespace app\admin\controller;

use think\Db;
use think\Session;

class  user  extends Permissions
{

    /**
    * 会员列表
    */
    public function index()
    {
        $post = $this->request->param();
        $res = Session::get("admin_cate_id");
        if (isset($post['keywords']) and !empty($post['keywords'])) {
            $where['NickName'] = ['like', '%' . $post['keywords'] . '%'];
        }
        if (isset($post['userid']) and !empty($post['userid'])) {
            $where['UserID'] = ['like', '%' . $post['userid'] . '%'];
        }
        $account    =   getSqlConnec('QPAccountsDB_CHUI', 'AccountsInfo');
        $data = empty($where) ? $account->order('UserID desc')
            ->paginate(20) : $account->order('UserID desc')
            ->where($where)->paginate(20,false,['query'=>$this->request->param()]);


        $user   =   [];
        foreach($data as $k=>$v){
            $treasure   =   getSqlConnec('QPTreasureDB_CHUI', 'GameScoreInfo');
            $user[$k]['UserID']     =   $v['UserID'];
            $user[$k]['NickName']   =   $v['NickName'];
            $user[$k]['Compellation']=   $v['Compellation'];
            $user[$k]['PassPortID'] =   $v['PassPortID'];
            $user[$k]['Nullity']    =   $v['Nullity'];
            $user[$k]['RegisterDate']=   $v['RegisterDate'];

            $user_treasure          =   $treasure->where(['UserID' => $v['UserID']])->field('Score, InsureScore')->find();
            $user[$k]['Score']      =   $user_treasure['Score'];
            $user[$k]['InsureScore']=   $user_treasure['InsureScore'];

            $invitation             =   Db::name('relation')->where(['game_user_id' => $v['UserID']])->find();
            $user[$k]['invitation_code']    =   empty($invitation) ? '' : $invitation['invitation_code'];
        }
        $this->assign(['user' => $user, 'res' => $res, 'data' => $data]);
        return $this->fetch();
    }

    /**
     * 删除会员
     */
     /*public function delete(){
        $id = input('id');
         $lis = Db::connect("QPAccountsDB_CHUI")->table('AccountsInfo')->where("UserID='$id'")->delete();
         $list = Db::name('relation')->where('game_user_id',$id)->find();

         if(!$list){
             if($lis){
            return $this->success('删除成功','admin/user/index');
         }else{
            return $this->error('删除失败');
         }
       }else {
             $list = Db::name('relation')->where('game_user_id', $id)->delete();
             if ($lis) {
                 if ($list) {
                     return $this->success('删除成功', 'admin/user/index');
                 } else {
                     return $this->error('删除失败');
                 }

             }
         }
     }*/

     //
  public function update(){
        $id = input('id');
        if(!$id){
            return $this->error('缺少参数');
        }
         $lis = getSqlConnec('QPAccountsDB_CHUI', 'AccountsInfo')->where("UserID='$id'")->value('Nullity');
         if($lis==0){
            $data['Nullity']=1; 
         }else{
            $data['Nullity']=0; 
         }
         $res = getSqlConnec('QPAccountsDB_CHUI', 'AccountsInfo')->where("UserID='$id'")->update($data);
         if($res){
             return $this->success('修改成功','admin/user/index');
         }else{
            return $this->error('修改失败');
         }
     }
     //Score房卡

    public function fknum()
    {
        $id= input('id');
        $this->assign('id',$id);
        return $this->fetch();
    }
     public function fangka(){
         $id = input('id');
         $data= input('money');
         if(!$id || !getSqlConnec('QPTreasureDB_CHUI', 'GameScoreInfo')->where("UserID='$id'")->find()) {
             return $this->error('缺少参数');
         }
         $res = getSqlConnec('QPTreasureDB_CHUI', 'GameScoreInfo')->where("UserID='$id'")->setInc('Score',$data);
         if($res){
             return $this->success('充值成功','admin/user/index');
         }else{
            return $this->error('充值失败');
         }
     }

    /**
     * 旗力币充值
     * @return mixed
     */
    public function qilibi()
    {
        $id=input('id');
        $this->assign('id',$id);
        return $this->fetch();
    }

    //InsureScore旗力币
    public function personal()
    {
        $res    =   $this->request->param();
        if(!$res['id'] || !getSqlConnec('QPTreasureDB_CHUI', 'GameScoreInfo')->where(["UserID" => $res['id']])->find()) {
            return $this->error('缺少参数');
        }

        $fangka =   $res['money'];
        if (!$fangka){
            return $this->error('旗力币数输入为空');
        }
        if(!preg_match("/^[1-9]\d*$/",$fangka)){
            return $this->error('请输入整数');
        }
        $res = getSqlConnec('QPTreasureDB_CHUI', 'GameScoreInfo')->where(["UserID" => $res['id']])->setInc('InsureScore', intval($fangka));
        if($res){
            return $this->success('充值成功','admin/user/index');
        }else{
            return $this->error('充值失败');
        }
    }

    public function qynum()
    {
        $id=input('id');
        $res = Db::name('relation')->where('game_user_id',$id)->value('invitation_code');
        $this->assign('res',$res);
        $this->assign('id',$id);
        return $this->fetch();
    }

    public function alert()
    {
        $id = input('id');
        if(!$id)
        {
            return $this->error('缺少参数');
        }
        $res = input('qynum');
        if (!$res)
        {
            return $this->error('邀请码不能为空');
        }
        $lis = Db::name('ratio')->where('request',$res)->find();
        if (!$lis)
        {
            return $this->error('邀请码不存在');
        }
        $data['invitation_code']= $res;
        $list= Db::name('relation')->where('game_user_id',$id)->update($data);
        if ($list)
        {
            return $this->error('修改成功','admin/use/index');
        }else{
            return $this->error('修改失败');
        }
    }

} 